/**
 * @file ZuoWifi.c
 * @author 左~~
 * @brief 用于wifi通信接口与数据包操作
 * @version 0.1
 * @date 2024-11-22
 * 
 * @copyright Copyright (c) 2024
 * 
 */

/////////////////////////// / DATA / ////////////////////////////////////



/////////////////////////// END DATA ////////////////////////////////////

#include "ZuoWifi.h"
#include "main.h"
#include "usart.h"


/// @brief 向上发送字符串,总长不允许超过250
/// @param str 字符串
/// @attention 该过程全阻塞 !
/// @class priveta
void Wifi_SendData(char str[])
{
    int len=-1;
    u8 temp=0xFF;
    //HAL_UART_Transmit(&huart2,&temp,1,1000);
    while(str[len++]!='\n' )
    {
        temp=str[len];

        HAL_UART_Transmit(&UART3_Handler,&temp,1,500);

    
    }
        

}

/// @brief 发送数据打包
/// @param head json数据头 名称只能为两位
/// @param num 数据本体 必须是三位数
/// @attention 该过程全阻塞 ! 
void Wifi_Send(char head[],int num)
{
    char temp[]="{\"xx\":NNN}\r\n";
   // Wifi_SendData();
   uint8_t temp2[]="AT+MQTTPUBRAW=0,\"/k1yv9Z7My1s/Demo/user/esp8266\",12,1,0\r\n";
    HAL_UART_Transmit(&UART3_Handler,temp2,58,1000);
    temp[2]=head[0];
    temp[3]=head[1];
    temp[6]=(num/100)+'0';
    temp[7]=((num/10) % 10)+'0';
    temp[8]=(num%10) + '0';
    HAL_Delay(300);
    //Wifi_SendData(temp);
    HAL_UART_Transmit(&UART3_Handler,temp,13,1000);
    //HAL_Delay(200);
    // u8 contemp[] = "AT+MQTTPUBRAW=0,\"/k1yv9Z7My1s/Demo/user/esp8266\",\"{\"xx\":NNN}\",1,0\r\n";
    //      HAL_UART_Transmit(&UART3_Handler,contemp,68,1000);

}

/// @brief 
/// @param head 
void Wifi_SendErr(char head[])
{
    char temp[]="{\"xx\":000}\r\n";
    Wifi_SendData("AT+MQTTPUBRAW=0,\"/k1yv9Z7My1s/Demo/user/esp8266\",12,1,0\r\n");
    temp[2]=head[0];
    temp[3]=head[1];

    HAL_Delay(500);
    Wifi_SendData(temp);
}


/// @brief 用于初始化Wifi模块
/// @param  void
/// 
void Wifi_Init()
{
    //HAL_Delay(1500);
    Wifi_SendData("AT+RST\r\n");
    HAL_Delay(1500);
 //   LCD_Clear(YELLOW);
    Wifi_SendData("AT+CWJAP=\"Red\",\"12344321\"\r\n");
    HAL_Delay(2000);
 //   LCD_Clear(GREEN);
    Wifi_SendData("ATE0\r\n");
    HAL_Delay(1500);
 //   LCD_Clear(BLUE);
    Wifi_SendData("AT+MQTTUSERCFG=0,1,\"NULL\",\"Demo&k1yv9Z7My1s\",\"d6acad3b11880e2414fec7a3f36dba18b5c4fbff7259d47373c6c885f05737b7\",0,0,\"\"\r\n");
    HAL_Delay(1500);
  //  LCD_Clear(RED);
    Wifi_SendData("AT+MQTTCLIENTID=0,\"CLIENTID\"\r\n");
    HAL_Delay(1500);
 //   LCD_Clear(BLACK);
    Wifi_SendData("AT+MQTTCLIENTID=0,\"k1yv9Z7My1s.Demo|securemode=2\\,signmethod=hmacsha256\\,timestamp=1731131581319|\"\r\n");
    HAL_Delay(1500);
    Wifi_SendData("AT+MQTTCONN=0,\"iot-06z00fbyg55hhrr.mqtt.iothub.aliyuncs.com\",1883,1\r\n");
    HAL_Delay(500);
    Wifi_SendData("AT+MQTTSUB=0,\"/k1yv9Z7My1s/WechatWechat/user/esp8266\",1\r\n");
    HAL_Delay(500);



   
}


/// @brief 网络下行数据接口
/// @param cal_function 执行回调函数
/// @param  str 要解析的字符串
/// @attention 解析异常时,不会进行调用
void Wifi_UnZiper(void cal_function(unsigned char head , int num),unsigned char str[])
{
    printf("startUnzip");
    char temp=-1;
    char tmep1=0;
    char temp2=0;
    while(str[++temp]!='{')
        if(str[temp]=='\n')
            return;
    
    ///{   "  x  x  H  "  :  N  N   N
    //temp 1  2  3  4  5  6  7  8   9
    if(str[temp + 4] > '5')
        return;
    printf("get!!");
    cal_function(str[temp+4],(int)(str[temp+7]-'0')*100 + (str[temp+8]-'0')*10 + (str[temp+9]-'0'));


}

